<?php
/**
 * 促销抓取控制器
 * @author hongru
 *
 */
class Controller_Admin_Spider extends Controller_Template 
{
	private $_limit = 10;
	
	public function __construct ($req, $res)
	{
		$this->template = 'smarty:admin/spider/list';
		$limit = (int) Kohana::config('spider.list_page_size');
		$limit && $this->_limit = $limit;
		parent::__construct ($req, $res);
	}
	
	/**
	 * 
	 */
	public function action_index ()
	{
		$this->action_list();
	}

	/**
	 * 促销列表页
	 */
	public function action_list(){
		$page = (int) $this->request->query('page') ? (int) $this->request->query('page') : 1;
		$offset = ($page - 1) * $this->_limit;
		$result = Cuxiao_Spider::search(null,$offset,$this->_limit);
				
		// set variable
		$this->template->set('page_title','促销抓取列表页');
		$this->template->set('total',$result['count']);
		$this->template->set('list',$result['list']);
		$this->template->set('pagination',Pagination::factory(array('total_items'=> $result['count'],'items_per_page'=> $this->_limit)));	
	}
	
	/**
	 * 促销信息编辑页面
	 */
	public function action_edit($id = null){
		// 检查参数
		$id = (int) $id;
		if(!$id || ( !$data = Cuxiao_Spider::getOne($id)) || $data['t_status'] == 'import' ) $this->request->redirect('/spider/list');

		$this->cuxiao_edit($data);
		$this->template->set('id',$id);
		$this->template->set('form_editor', editor::build('goods_description', $data['descript'], $this->editor_config('goods_desc')));
		$this->template->set('page_title','促销抓取编辑');
		$this->template->set_filename('smarty:admin/spider/edit');
	} 
	
	/**
	 * 促销编辑保存
	 */
	public function action_editSave($id = null){
		$id = (int) $id;
		if(!$id || !$data = Cuxiao_Spider::getOne($id) ) $this->request->redirect('/spider/list');
		
		Cuxiao_Spider::editSave($id,$this->request->post());
		$this->request->redirect("/spider/edit/$id");
		exit();
	}

	/**
	 * 导入指定ID的促销信息到线上
	 * 共五种错误：
	 *    admin_spider.not_exists(110005)
	 *    admin_spider.created_shop_falid(110010)
	 *    admin_spider.not_spider_created_shop(110012)
	 *    admin_spider.improt_falid(110008)
	 *    global_ok(100000)
	 */
	public function action_import_online(){
		// 检查参数
// 		$id = (int) $this->request->post('id');
		$id = (int) $_REQUEST['id'];
		if(!$id || ( !$data = Cuxiao_Spider::getOne($id) ) || $data['t_status'] == 'import' ) exit(Js_Reponse::jsonError(null,'admin_spider.not_exists'));
		
		// 店铺处理
		$status = Shop_Spider::shop_info($data['shop_name'],$data);
		$status['status'] != 'admin_spider.spider_created_shop' && exit(Js_Reponse::jsonError(null,$status['status'],array('shop_name'=>$data['shop_name'])));
		$shop_info = $status['shop_info'];
		
		// 导入促销信息
		if($cuxiao_id = Cuxiao_Spider::saveToOnlie($id,$shop_info['id'],$data)){
			exit(Js_Reponse::jsonSucc(array('url'=>'http://'.site::domain('member')."/cuxiao/info/{$cuxiao_id}"),import_success));
		}
		exit(Js_Reponse::jsonError(null,'admin_spider.improt_falid'));
	}

	/**
	 * 促销编辑表单数据
	 */
	private function cuxiao_edit($data){
		// 时间
		$start_date = $data['start_time'] ? date('Y-m-d',$data['start_time']) : date('Y-m-d',time());
		$end_date = $data['end_time'] ? date('Y-m-d',strtotime("+28 day")) : date('Y-m-d',time());
		$detail = array('start_date' => $start_date,'end_date' => $end_date);
		
		// 电话
// 		$shop_id = 100011;
// 		$phones  = Shop_info::get_phones($shop_id);
// 		utilityHelper::format_online_phone($phones);

		// 分类品牌
		$category = new Category_System();
		$cate_sys_1 = $category->get_list();
		if (!empty($data['class1']))
		{
			$cate_sys_2 = $category->get_list($data['class1']);
			$brand_list = $this->get_brand($data['class1'],1);
		}
		else
		{
			$cate_sys_2 = NULL;
			$brand_list = NULL;
		}
		
		if (!empty($data['class2']))
		{
			$cate_sys_3 = $category->get_list($data['class2']);
			$brand_list = $this->get_brand($data['class2'],2);
		}
		else
		{
			$cate_sys_3 = NULL;
			$brand_list = NULL;
		}
		
		// 省市区商圈
		$provinces = $citys = $countys = $shangquans = array();
		$provinces   = City::province();
		
		(int) $data['province'] && $citys =  City::cities((int) $data['province']);
		(int) $data['city'] && $countys = City::county((int) $data['city']);
		(int) $data['county'] && $shangquans = City::shangquan((int) $data['county']);
		
		$this->template->set('provinces',$provinces);
		$this->template->set('citys',$citys);
		$this->template->set('countys',$countys);
		$this->template->set('shangquans',$shangquans);
		$this->template->set('category_sys_1',$cate_sys_1);
		$this->template->set('category_sys_2',$cate_sys_2);
		$this->template->set('category_sys_3',$cate_sys_3);
		$this->template->set('brand_list',$brand_list);
		
		$this->template->set('detail',$data+$detail);
	}
	
	/**
	 * 编辑器控制方法
	 * @param  $type
	 * @return string
	 */
	private function editor_config ($type)
	{
		$editor_config = Kohana::config('shop.goods.editor');
		$uid           = user::current_user_id();
		$type          = trim($type);
		$input_name    = 'upload';
		$image_domain  = site::domain('imgup');
	
		$editor_config['filebrowserUploadUrl'] = "http://{$image_domain}/upimage/uploads/editor_images?target_id={$uid}&target_type={$type}&input_name={$input_name}";
	
		return $editor_config;
	}
	
	/*
	 * 通过系统分类获取品牌列表
	*/
	private function get_brand($cate_id,$level)
	{
		if (!empty($cate_id) && !empty($level))
			$data = Category_Brand::get_by_category_level($cate_id,$level);
	
		$brands= array();
		if(isset($data))
		{
			foreach ($data as $r)
			{
				$brands[] = array(
						'id' => $r['brand_id'],
						'name' => $r['name'],
				);
			}
		}
		return $brands;
	}
}
	
